#include <QApplication>
#include <QDesktopWidget>
#include <QToolBar>
#include "drawingdesigner.h"
#include "drawingsurface.h"
#include "forms/configuration.h"
#include "actionpool.h"

namespace ShUi
{

class DrawingDesignerImpl : public Impl<DrawingDesigner>
{
public:
    void init();
    void exit();
    void createToolBar();
    bool _fullscreen;
    DrawingSurface* _surface;
    QToolBar* _toolBar;
    ActionPool* _actionPool;
};

void DrawingDesignerImpl::init()
{
    SH_Q(DrawingDesigner);
    _fullscreen = false;

    _actionPool = new ActionPool(q);
    _actionPool->createActions();
    q->connect(_actionPool, SIGNAL(commandIssued(const QString&)), q, SLOT(onCommandIssued(const QString&)));

    _surface = new DrawingSurface(q);
    q->setCentralWidget(_surface);

    createToolBar();
}

void DrawingDesignerImpl::exit()
{
}

void DrawingDesignerImpl::createToolBar()
{
    SH_Q(DrawingDesigner);
    _toolBar = q->addToolBar(q->tr("tool"));
    QAction* action = _actionPool->action("config");
    _toolBar->addAction(action);
}

DrawingDesigner::DrawingDesigner()
{
    SH_I(DrawingDesigner);
    d->init();
    setMinimumSize(500, 500);
}

DrawingDesigner::~DrawingDesigner()
{
   SH_E(DrawingDesigner);
   d->exit(); 
}

DrawingSurface* DrawingDesigner::surface() const
{
    SH_D(const DrawingDesigner);
    return d->_surface;
}

void DrawingDesigner::present()
{
    SH_D(DrawingDesigner);
    if (d->_fullscreen)
    {
        showMaximized();
	// setWindowState(Qt::WindowFullScreen);
    }
    else
    {
	/*
        QPoint center = QApplication::desktop()->availableGeometry(this).center();
        int wd = frameGeometry().width() / 2;
        int ht = frameGeometry().height() / 2;
        move(center.x() - wd, center.y() - ht);
	*/
        show();
    }
}

void DrawingDesigner::setFullscreen(bool fullscreen)
{
    SH_D(DrawingDesigner);
    d->_fullscreen = fullscreen;
}

bool DrawingDesigner::isFullscreen() const
{
    SH_D(const DrawingDesigner);
    return d->_fullscreen;
}

void DrawingDesigner::contextMenuEvent(QContextMenuEvent*)
{
}

void DrawingDesigner::onCommandIssued(const QString& command)
{
    if (command == "config")
    {
        static Configuration* config = SH_NULL;
        if (config)
            SH_SAFE_DELETE(config);
        config = new Configuration(this);
        config->show();
    }
}

} // namespace ShUi
